package controller;

import logController.Logger;
import UI.MainWindow;

/**
 * @date 27, March 2012.
 * @author Francisco J. Martínez
 * @description DaddySmurf Controller like is name said, will be the main controller class.
 * All controller classes will be placed here for a easy access.
 * Each siblings class will contains a instance of this DaddySmurf class.
 * Maybe exists more efficient ways to do that. But anyway do it yourself!
 * 
 * Singleton design pattern implemented here.
 */
public class DaddySmurfController implements IMainController{
	
	
	private static IMainController mSingleton = null;
	/**
	 * All variables and objects will be private identifier, and no set methods. 
	 */
	
	private IEditorsController mEditorController;
	private ITreeController mTreeController;
	private ILogController mLogController;
	private IMenuController mMenuController;
	
	private MainWindow mMainView;
	
	/**
	 * Private constructor for Singleton pattern
	 */
	private DaddySmurfController(){
		// Initialize the log
		Logger.startLog();
		
		mEditorController = new EditorsController(this);
		mMenuController = new MenuController(this);
	}
	
	/**
	 * The only way to access this class
	 * @return a DaddySmurf class 
	 */
	public static synchronized IMainController getInstance(){
		if (mSingleton == null){
			mSingleton = new DaddySmurfController();
		}
		return mSingleton;
	}

	/**
	 * 
	 * @return the TextEditorController
	 */
	public IEditorsController getTextEditorController() {
		return mEditorController;
	}

	public ITreeController getTreeController() {
		return mTreeController;
	}

	public ILogController getLogController() {
		return mLogController;
	}

	public IMenuController getMenuController() {
		return mMenuController;
	}

	/**
	 * @param s The string to load in the simple text editor mode 
	 */
	@Override
	public void loadTextIntoTextEditor(String s) {
		this.mEditorController.loadText(s);
	}

	@Override
	public String getTextOfTheBasicEditor() {
		return this.mEditorController.getText();
	}
	/**
	 * Le asigna una vista al controlador y este crea asocia los controladores a las subvistas
	 */
	@Override
	public void setView(MainWindow mainView) {
		this.mMainView = mainView;
		assingViewsToControllers();
	}

	private void assingViewsToControllers(){
		mEditorController.setView(mMainView.getEditorsUI());
		mMenuController.setView(mMainView.getMenuBarUI());
	}

}
